<?php
require_once('config.php');

/*$testi=mysql_query("SELECT * FROM Race", $conn);
var_dump(mysql_fetch_array($testi));
*/

$char=explode("\n",$_REQUEST['data']);

$skills=array();
$classes=array();
$spells=array();
$spell=array();
$feats=array();
$equipment=array();
$language=array();

foreach ($char as $key){
	$tmp=explode(" ", $key);
	switch ($tmp[0]){
		case "Player":
			if ($playerID=exists_in_db($tmp[1]))
				remove_player($playerID);
			$tdb["pname"]=$tmp[1];
			break;
		case "Name":
			$tdb["cname"]=$tmp[1];
			break;	
		case "RaceNum":
			$tdb["race"]=$tmp[1];
			break;
		case "StartingClass":
			$tdb["sclass"]=$tmp[1]+1;
			break;
		case "Barbarian":
		case "Bard":
		case "Cleric":
		case "Druid":
		case "Fighter":
		case "Monk":
		case "Paladin":
		case "Ranger":
		case "Rogue":
		case "Sorcerer":
		case "Wizard":
			$class["name"]=$tmp[0];
			$class["value"]=$tmp[1];
			array_push($classes, $class);
			$lvl=$lvl+$tmp[1];
			break;
		case "Alignment":
			$tdb["align"]=$tmp[1];
			break;
		case "Religion":
			$tdb["religion"]=$tmp[1]+1;
			break;
		case "Strength":
		case "Dexterity":
		case "Constitution":
		case "Intelligence":
		case "Wisdom":
		case "Charisma":
			$attrib[$tmp[0]]=$tmp[1];
			break;
		case "Money":
			$tdb["money"]=$tmp[1]+1;
			break;
		case "BaseAttackBonus":
			$tdb["BAB"]=$tmp[1];
			break;
		
		case "FortitudeSave":
			$tdb["fortsv"]=$tmp[1];
			break;
		case "WillSave":
			$tdb["willsv"]=$tmp[1];
			break;
		case "ReflexSave":
			$tdb["refsv"]=$tmp[1];
			break;
		case "HitPoints":
			$tdb["hp"]=$tmp[1]+1;
			break;
/*		case "ArmourBonus":
			$tdb["armourbonus"]=$tmp[1]+1;
			break;
		case "ArmourIndex":
			$tdb["equip_armor"]=$tmp[1]+1;
			break;
		case "ShieldIndex":
			$tdb["equip_shield"]=$tmp[1]+1;
			break;*/
		case "Feat":
			array_push($feats, $tmp[1]+1);
			break;
		case "Language":
			array_push($language, $tmp[1]+1);
			break;
		case "Spell":
			$spell["lvl"]=$tmp[2];
			$spell["id"]=$tmp[3];
			$spell["class"]=$tmp[1];
			array_push($spells, $spell);
			break;
		case "Skill":
			$skill["index"]=$tmp[1]+1;
			$skill["value"]=$tmp[2];
			array_push($skills, $skill);
			break;
		case "EQ":
			$item["id"]=$tmp[1]+1;
			$item["amount"]=$tmp[2];
			$item["bonus"]=$tmp[3];
			array_push($equipment, $item);
			break;
		default:
			break;		
	}
	
}

//var_dump($skills);

$query=
"INSERT INTO `Player`". 
"(`player_id`, `p_name`, `c_name`, `race_id`, `relign_id`, `align_id`, `exp`, `lvl`, `hp`, `str`, `dex`, `con`, `int`, `wis`, `cha`, `gp`, `fort_save`, `ref_save`, `will_save`, `base_attack_bonus`) VALUES ".
"(NULL, '".$tdb["pname"]."', '".$tdb["cname"]."', '".$tdb["race"]."', '".$tdb["religion"]."', '".$tdb["align"]."', '".$tdb["exp"]."', '".$lvl."', '".$tdb["hp"]."', '".$attrib["Strength"]."', '".$attrib["Dexterity"]."', '".$attrib["Constitution"]."', '".$attrib["Intelligence"]."', '".$attrib["Wisdom"]."', '".$attrib["Charisma"]."', '".$tdb["money"]."', '".$tdb["fortsv"]."', '".$tdb["refsv"]."', '".$tdb["willsv"]."', '".$tdb["BAB"]."')";
mysql_query($query);
$player_id=mysql_affected_rows();
echo mysql_error();

foreach ($skills as $skill){
	$query="INSERT INTO P_Skill (player_id, skill_id, value) VALUES ($player_id, ".$skill["index"].", ".$skill["value"].")";
	mysql_query($query);	
}
foreach ($feats as $feat){
	$query="INSERT INTO P_Feat (player_id, feat_id) VALUES ($player_id, $feat)";
	mysql_query($query);
}

foreach ($classes as $class){
	if ($class["value"]>0){ //if value is 0 there is no levels on the class
		$query="INSERT INTO P_Class (player_id, class_id, value) VALUES".
		"($player_id, (SELECT class_id FROM Class WHERE name LIKE \"".$class["name"]."\"), ".$class["value"].")"; 
		mysql_query($query);
	}
}

foreach ($spells as $spell){
	if ($spell["class"]=="Sorcerer"){
		$spell["class"]="Wizard";
	}
	$query="INSERT INTO P_Spell (player_id, spell_id) VALUES ($player_id, (SELECT spell_id FROM Spell INNER JOIN Class ON Class.class_id = Spell.class_id WHERE Class.name=\"".$spell["class"]."\" AND Spell.level=".$spell["lvl"]." AND Spell.level_id=".$spell["id"]."));";
	mysql_query($query);
	echo mysql_error()."<br/>";
}

foreach ($equipment as $item){
	$bonus_id="NULL";
	
	if ($item["bonus"] != ""){ //only masterwork weapons etc. can be acquired in char gen. Rest is granted by GM
		$bonus_id=1;
	}
	$query="INSERT INTO P_Equip (player_id, equip_id, amount, bonus_id) VALUES ($player_id, ".$item["id"].", ".$item["amount"].", $bonus_id)";
	mysql_query($query);

	echo mysql_error()."<br/>";

	
}

foreach ($language as $key){
	$query="INSERT INTO P_Lang (player_id, lang_id) VALUES ($player_id, $key)";
	mysql_query($query);
	echo mysql_error()."<br/>";
}



//echo $query;
//var_dump($atrrib);
mysql_close($conn);

function add_db($query){
	mysql_query($query);
}
function exists_in_db($pname){
	if (mysql_query("SELECT player_id FROM player WHERE p_name=$pname"))
		return 1;
	else
		return 0;
}
function remove_player($player_id){}
?>

